package com.yijiu.trb;

import android.content.Intent;
import android.os.Bundle;
import android.text.Html;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.TextView;

import com.yijiu.action.BaseAction;
import com.yijiu.network.Request;
import com.yijiu.network.StringUtils;
import com.yijiu.resource.ActionType;
import com.yijiu.resource.C;
import com.yijiu.resource.CacheData;
import com.yijiu.response.RequestModifyPasswordResponse;

public class ChangePasswordActivity extends BTitleActivity implements OnClickListener
{
	/**
	 * 旧密码编辑框
	 */
	private EditText oldPasswordEditText;
	
	/**
	 * 新密码编辑框
	 */
	private EditText newPasswordEditText;
	
	/**
	 * 确认新密码编辑框
	 */
	private EditText affirmNewPasswordEditText;
	
	/**
	 * 确认修改
	 */
	private Button affirmModifyButton;
	
	/**
	 * 确认新密码
	 */
	private String affirmNewPassword;
	
	/**
	 * 新密码
	 */
	private String newPassword;
	
	/**
	 * 旧密码
	 */
	private String oldPassword;
	
	private String changePwdType = "logPassword";
	
	private TextView forgetPasswordTextView;// 忘记支付密码
	
	private OnCheckedChangeListener changePwdTypeListener = new OnCheckedChangeListener()
	{
		@Override
		public void onCheckedChanged(RadioGroup group, int checkID)
		{
			switch (checkID)
			{
				case R.id.rb_change_pwd_login:
					changePwdType = "logPassword";
					oldPasswordEditText.setHint("请输入当前登录密码");
					newPasswordEditText.setHint("请输入新登录密码");
					affirmNewPasswordEditText.setHint("请再次输入新登录密码");
					forgetPasswordTextView.setVisibility(View.GONE);
					break;
				case R.id.rb_change_pwd_pay:
					changePwdType = "payPassword";
					oldPasswordEditText.setHint("请输入当前支付密码");
					newPasswordEditText.setHint("请输入新支付密码");
					affirmNewPasswordEditText.setHint("请再次输入新支付密码");
					forgetPasswordTextView.setVisibility(View.VISIBLE);
					break;
				default:
					break;
			}
			oldPasswordEditText.setText("");
			newPasswordEditText.setText("");
			affirmNewPasswordEditText.setText("");
		}
	};
	
	@Override
	public void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_change_pwd);
		initWidget();
	}
	
	@Override
	protected void initWidget()
	{
		super.initWidget();
		titleView.setText("修改密码");
		oldPasswordEditText = (EditText) findViewById(R.id.et_change_pwd_old);
		newPasswordEditText = (EditText) findViewById(R.id.et_change_pwd_new);
		affirmNewPasswordEditText = (EditText) findViewById(R.id.et_change_pwd_new_again);
		affirmModifyButton = (Button) findViewById(R.id.btn_change_pwd_submit);
		RadioGroup rgChangePwd = (RadioGroup) findViewById(R.id.rg_change_pwd);
		RadioButton login = (RadioButton) findViewById(R.id.rb_change_pwd_login);
		RadioButton pay = (RadioButton) findViewById(R.id.rb_change_pwd_pay);
		rgChangePwd.setOnCheckedChangeListener(changePwdTypeListener);
		forgetPasswordTextView = (TextView) findViewById(R.id.tv_change_pwd_forget_pay_password);
		forgetPasswordTextView.setOnClickListener(this);
		/** 调用Html属性添加下划线 */
		forgetPasswordTextView.setText(Html.fromHtml("<u>" + "忘记支付密码" + "</u>" + "？"));
		forgetPasswordTextView.setTextColor(getResources().getColor(R.color.red));
		affirmModifyButton.setOnClickListener(this);
	}
	
	/**
	 * 请求修改密码
	 * 
	 * @param appKey
	 */
	private void RequestModifyPassword(String appKey)
	{
		oldPassword = oldPasswordEditText.getText().toString();
		newPassword = newPasswordEditText.getText().toString();
		affirmNewPassword = affirmNewPasswordEditText.getText().toString();
		// 判断用户是否输入
		if (StringUtils.isNull(oldPassword))
		{
			app.showMsg("当前密码不能为空");
			return;
		}
		if (StringUtils.isNull(newPassword))
		{
			app.showMsg("新密码不能为空");
			return;
		}
		if (StringUtils.isNull(affirmNewPassword))
		{
			app.showMsg("新密码不能为空");
			return;
		}
		if (!StringUtils.checkHasLetNum(newPassword)
				|| !StringUtils.checkHasLetNum(affirmNewPassword)
				|| ((newPassword.length() < 6 || newPassword.length() > 16) || (affirmNewPassword.length() < 6 || affirmNewPassword
						.length() > 16)))
		{
			app.showMsg("密码应该为6~16位字符，至少包含数字、字母（区分大小写）");
			return;
		}
		if (!affirmNewPassword.equals(newPassword))
		{
			app.showMsg("两次输入的密码不一样");
			return;
		}
		// 将用户输入的信息拼接成字符串发送到服务器
		String inputStr = "service=modifyPasswordService&oldPassword=" + oldPassword + "&newPassword=" + newPassword
				+ "&newPasswordTo=" + affirmNewPassword + "&modifyType=" + changePwdType;
		// 用私钥进行AES加密
		encryptBase(inputStr, C.PRIVATE_KEY);
		// 将用公钥加密后的私钥、用私钥加密后的数据进行封装
		BaseAction action = new BaseAction(encodedData, encriptData, appKey);
		// 登录响应
		RequestModifyPasswordResponse requestModifyPasswordResponse = new RequestModifyPasswordResponse();
		// 请求服务器
		Request request = new Request(action, requestModifyPasswordResponse, ActionType.REQUEST_MODIFY_PASSWORD);
		netManager.excute(request, this, this);
		showProgress("正在修改密码，请稍候");
	}
	
	/* 操作成功返回的函数 */
	@Override
	public void getResult(Request r)
	{
		super.getResult(r);
		switch (r.getType())
		{
			case ActionType.REQUEST_MODIFY_PASSWORD:
				app.showMsg("修改成功！");
				finish();
				break;
		}
	}
	
	/* 操作失败返回的函数 */
	@Override
	public void notifyError(int type, int errorCode, String errorMsg)
	{
		super.notifyError(type, errorCode, errorMsg);
		switch (type)
		{
			case ActionType.REQUEST_MODIFY_PASSWORD:
				app.showMsg(errorMsg);
				break;
		}
	}
	
	/* 重复提交返回的函数 */
	@Override
	public void rePost(Request r)
	{
		super.rePost(r);
	}
	
	@Override
	public void onClick(View arg0)
	{
		if (arg0.getId() == R.id.btn_change_pwd_submit)
		{
			String appKey = CacheData.APP_KEY;
			RequestModifyPassword(appKey);
		}
		else if (arg0.getId() == R.id.tv_change_pwd_forget_pay_password)
		{
			Intent intent = new Intent(this, ForgetPwdTwoActivity.class);
			intent.putExtra("userName", CacheData.USER_NAME);
			intent.putExtra("mobile", CacheData.MOBILE_NUMBER);
			intent.putExtra("flag", "pay");
			startActivity(intent);
			finish();
		}
	}
}
